<template>
  <div>
    <content-box one="商品管理" two="参数列表">
      <!-- 警告区域 -->
      <el-alert
        title="注意：只允许第三级分类设置相关参数"
        type="warning"
        show-icon
        :closable="false"
      ></el-alert>
      <!-- 选择商品属性 -->
      <el-row>
        <el-row>
          <span>选择商品分类: </span>
          <!-- 选择商品分类的级联选择框 -->
          <el-cascader
            v-model="selectCatesKey"
            :options="paramsList"
            :props="cascaderProps"
            @change="parentCatChanged"
            clearable
          ></el-cascader>
        </el-row>
      </el-row>
      <!-- tab 页签区域 -->
      <el-tabs v-model="activeName" @tab-click="handleTabClick">
        <!-- 添加动态参数面板 -->
        <el-tab-pane label="动态参数" name="many">
          <!-- 添加动态参数按钮 -->
          <el-button type="primary" size="mini" :disabled="isBtnDisabled"
            >添加参数</el-button
          >
          <!-- 动态表格 -->
          <el-table :data="manyTableData" style="width: 100%" border stripe>
            <!-- 展开行 -->
            <el-table-column type="expand"></el-table-column>
            <!-- 索引列 -->
            <el-table-column type="index"></el-table-column>
            <el-table-column prop="attr_name" label="参数名称">
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scoped">
                <el-button type="primary" icon="el-icon-edit" size="mini"
                  >编辑</el-button
                >
                <el-button type="danger" icon="el-icon-delete" size="mini"
                  >删除</el-button
                ></template
              >
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <!-- 添加静态属性参数面板 -->
        <el-tab-pane label="静态属性" name="only">
          <!-- 添加静态属性按钮 -->
          <el-button type="primary" size="mini" :disabled="isBtnDisabled"
            >添加属性</el-button
          ><!-- 静态参数 -->
          <el-table :data="manyTableData" style="width: 100%" border stripe>
            <!-- 展开行 -->
            <el-table-column type="expand"></el-table-column>
            <!-- 索引列 -->
            <el-table-column type="index"></el-table-column>
            <el-table-column prop="attr_name" label="属性名称">
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scoped">
                <el-button type="primary" icon="el-icon-edit" size="mini"
                  >编辑</el-button
                >
                <el-button type="danger" icon="el-icon-delete" size="mini"
                  >删除</el-button
                ></template
              >
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </content-box>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //商品分类列表
      paramsList: [],
      //级联选择框的配置对象
      cascaderProps: {
        expandTrigger: "hover",
        multiple: false,
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },
      //级联选择框双向绑定到的数组
      selectCatesKey: [],
      //被激活的页签的名称
      activeName: "many",
      //动态参数的数据
      manyTableData: [],
      //静态属性的数据
      onlyTableData: {},
    };
  },
  created() {
    this.getParamsList();
  },
  methods: {
    async getParamsList() {
      const { data: res } = await this.$http.get("categories");
      if (res.meta.status !== 200)
        return this.$message.error("获取商品分类失败");
      // console.log(res.data);
      this.paramsList = res.data;
      console.log(this.paramsList);
    },
    //级联选择框选中项变化,会触发这个函数
    parentCatChanged() {
      //根据所选分类的Id，和当前所处的面板，获取对应的参数
      if (this.selectCatesKey.length !== 3) {
        this.selectCatesKey = [];
        return;
      }
      console.log(this.selectCatesKey);
      this.getParamsData();
    },
    handleTabClick() {
      console.log(this.activeName);
      this.getParamsData();
    },
    async getParamsData() {
      const { data: res } = await this.$http.get(
        `categories/${this.CatId}/attributes`,
        {
          params: { sel: this.activeName },
        }
      );
      if (res.meta.status !== 200) {
        return this.$message.error("获取失败");
      }
      console.log(res.data);
      if (this.activeName === "many") {
        this.manyTableData = res.data;
      } else this.onlyTableData = res.data;

      this.$message.success("获取成功");
    },
  },
  // 计算属性
  computed: {
    //如果按钮需要被禁用,则返回true,否则返回false
    isBtnDisabled() {
      if (this.selectCatesKey.length !== 3) {
        return true;
      }
      return false;
    },
    //当前选中的三级分类的Id
    CatId() {
      if (this.selectCatesKey.length === 3) {
        console.log(this.selectCatesKey[2]);

        return this.selectCatesKey[2];
      }
      //没有选中三级分类
      return null;
    },
  },
};
</script>

<style lang="less" scoped>
.el-row {
  margin: 15px 0;
}
</style>